package com.jxust.hr.security.handler;

import com.alibaba.fastjson.JSON;
import com.jxust.debate.util.ResultEnum;
import com.jxust.debate.util.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户登入失败处理逻辑
 *
 * @author tyeerth
 * @date 2020/12/18 - 15:28
 */
@Component
@Slf4j
public class AjaxAuthenticationFailureHandler implements AuthenticationFailureHandler {
    //    @Override
//    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
//        AjaxResponseBody responseBody = new AjaxResponseBody();
//
//        responseBody.setStatus("400");
//        responseBody.setMsg("Login 失败!");
//
//        httpServletResponse.getWriter().write(JSON.toJSONString(responseBody));
//    }
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.getWriter().write(JSON.toJSONString(ResultVO.result(ResultEnum.USER_LOGIN_FAILED, false)));
    }
}